﻿using System;
using System.Collections.Generic;

namespace HRX.PayCenter.Contract
{
    /// <summary>
    /// 基于约定扩展
    /// </summary>
    public static class ContractExtention
    {
        /// <summary>
        /// 获取签名摘要
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="init"></param>
        /// <param name="convertDict"></param>
        /// <param name="securityKey"></param>
        /// <returns></returns>
        public static string GetSign<T>(T data,  string securityKey) where T : class
        {
            IDictionary<string, string> dict = CommonUtils.ClassToMap(data, true);
            var signStr = CommonSecurity.GetSignContent(dict);

            return CommonSecurity.SignWithMD5(signStr + securityKey);
        }

        /// <summary>
        /// 获取加密字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="serialize"></param>
        /// <param name="securityKey"></param>
        /// <returns></returns>
        public static string GetEncrypt<T>(T data, Func<T, string> serialize, string securityKey)
        {
            string toEncrypt = serialize(data);

            return CommonSecurity.EncryptWithAES(toEncrypt, securityKey);
        }
    }
}
